<?php


namespace app\marketing\controller;
use app\marketing\model\Poster;
use app\marketing\model\PosterType;
use app\marketing\model\PosterType as PosterTypeModel;
use app\AdminRest;

class AdminPosterType extends AdminRest
{
    public function listPosterType()
    {
        $param = $this->_param;
        $page_config = array(
            'page' => 1,
            'page_count' => 20
        );
        //设置页码
        if (isset($param['page']) && $param['page'] > 0) $page_config['page'] = $param['page'];
        //设置每页的数据
        if (isset($param['page_count']) && $param['page_count'] > 0) $page_config['page_count'] = $param['page_count'];
        $filter[] = ['uniacid', '=', $this->_uniacid];

        $filter[] = ['status', '<>', -1];

        if (isset($param['type_id']) && $param['type_id'] > 0)
        {
            $filter[] = ['type_id', '=', $param['type_id']];
        }
        $m_poster_type = new PosterTypeModel();
        $page_config['total'] = $m_poster_type->listPosterTypesCount($filter);
        $posters = $m_poster_type->listPosterTypes($filter, $page_config);
        $page_config['total_page'] = (int)($page_config['total'] / $page_config['page_count']);
        if (($page_config['total'] % $page_config['page_count']) > 0) $page_config['total_page'] = $page_config['total_page'] + 1;
        //设置返回参数
        $result = $page_config;
        //返回数据
        $result['data'] = $posters;
        return $this->success($result);


    }

    public function listAllPosterType()
    {
        $filter[] = ['uniacid', '=', $this->_uniacid];
        $filter[] = ['status', '=', 1];

        $m_poster_type = new PosterTypeModel();
        $posters = $m_poster_type->field('id, title')->where($filter)->select()->toArray();

        //返回数据
        $result['data'] = $posters;
        return $this->success($result);
    }


    public function createPosterType()
    {
        $input = $this->_input;
        $title   = trim($input['title']) ?? null;

        if (empty($title)&&$title!=='0')
        {
            return $this->error('title is required');
        }

        if (mb_strlen($title) > 5) {
            return $this->error('title is to long');
        }

        $input['uniacid'] = $this->_uniacid;
        $poster_type = new PosterTypeModel();
        
//      var_dump(json_encode([['title', '=', $input['title'], ['uniacid', '=', $this->_uniacid]], ['status', '<>',  -1]] ,true));die;
        $check = $poster_type->listPosterTypesCount([['title' ,'=',$input['title']], ['uniacid' ,'=', $this->_uniacid],['status','>','-1']]);
        if (!empty($check))
        {
            return $this->error(lang('this type is exists'));
        }

        $res = $poster_type->createPosterType($input);
        return $this->success($res);
    }

    public function updatePosterType()
    {
        $poster_type_id   = $this->_param['id'] ?? null;
        if (!$poster_type_id)
        {
            return $this->error('poster_type_id is required');
        }

        $input = $this->_input;

        if (empty(trim($input['title']))&&trim($input['title'])!=='0')
        {

            return $this->error('title is required');
        }

        if (mb_strlen($input['title'] ?? '') > 5) {
            return $this->error('title is to long');
        }

        unset($input['status']);
        $poster_type = new PosterTypeModel();

        if ($poster_type->where([
            ['id', '<>', $poster_type_id],
            ['title', '=', $input['title']],
            ['uniacid', '=', $this->_uniacid],
            ['status', '<>',  -1]
        ])->count())
        {
            return $this->error('this type is exists');
        }

        $data = $poster_type->updatePosterType(['id' => $poster_type_id], $input);
        return $this->success($data);
    }

    public function getPosterType()
    {
        $poster_type_id   = $this->_param['id'];
        $poster_type = new PosterTypeModel();
        $data = $poster_type->getPosterType(['id' => $poster_type_id]);
        return $this->success($data);
    }

    public function changeStatus()
    {
        $poster_type_id   = $this->_param['id'] ?? null;
        if (!$poster_type_id)
        {
            return $this->error('poster_type_id is required');
        }
        $input = $this->_input;
//        $poster = new Poster();
//        if ($input['status'] != 1 && $poster->where(['type_id' => $poster_type_id, 'status' => 1, 'uniacid' => $this->_uniacid])->count())
//        {
//            return $this->error('该分类下有海报， 不能执行下架或者删除');
//        }
        $model = new PosterTypeModel();
        $rst = $model->updatePosterType(['id' => $poster_type_id], ['status' => $input['status']]);
        return $this->success($rst);
    }
}